package org.mech.sefer.client.ui.spring.task.impl;

import javax.annotation.Resource;

import org.apache.commons.lang.Validate;
import org.mech.sefer.client.ui.spring.task.ReflectionTask;
import org.mech.sefer.client.ui.spring.task.TaskRegistry;
import org.mech.sefer.client.ui.spring.task.TaskService;
import org.mech.sefer.client.ui.task.callback.TaskCallback;
import org.mech.sefer.client.ui.task.executor.TaskExecutor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * The TaskServiceImpl is simple implementation of {@link TaskService}
 * </p>
 * <p>
 * Date: 26.4.2012 10:42:09
 * </p>
 *
 * @author martin.krajc
 */
@Service("taskService")
public class TaskServiceImpl implements TaskService{
	
	@Resource(name="taskRegistry")
	private TaskRegistry registry;
	
	@Resource(name="taskExecutor")
	private TaskExecutor executor;

	@SuppressWarnings("unchecked")
	@Override
	public <T> void call(Class<?> interfaceClass, String methodName, TaskCallback<T> callBack, Object... args) {

		ReflectionTask templateTask = registry.getTask(registry.createKey(methodName, interfaceClass));
		
		Validate.notNull(templateTask);
		
		ReflectionTask task = new ReflectionTask();
		task.setMethod(templateTask.getMethod());
		task.setBean(templateTask.getBean());
		task.setArgs(args);
		
		executor.execute(task, callBack);
	}
}
